package com.itheima.service.mongo.impl;

import com.itheima.domain.mongo.RecommendUser;
import com.itheima.service.mongo.RecommendUserService;
import com.itheima.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ImportSelector;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@DubboService
public class RecommendUserServiceImpl implements RecommendUserService {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public RecommendUser findTodayBast(Long toUserId) {
        // 1.构建条件
        Query query = new Query(Criteria.where("toUserId").is(toUserId))
                .with(Sort.by(Sort.Order.desc("score")))
                .skip(0).limit(1);
        // 2.查询推荐用户
        RecommendUser recommendUser = mongoTemplate.findOne(query, RecommendUser.class);
        // 3.返回推荐用户
        return recommendUser;
    }

    // 推荐用户
    @Override
    public PageBeanVo findRecommendUserByPage(Long toUserId, Integer pageNum, Integer pageSize) {
        // 1.构建条件
        Integer index = (pageNum - 1) * pageSize + 1;
        Query query = new Query(Criteria.where("toUserId").is(toUserId))
                .with(Sort.by(Sort.Order.desc("score")))
                .skip(index).limit(pageSize);
        // 2.查询list
        List<RecommendUser> recommendUserList = mongoTemplate.find(query, RecommendUser.class);
        // 3.返回并封装分页对象
        return new PageBeanVo(pageNum, pageSize, 0l, recommendUserList);
    }

    // 二个人推荐缘分值
    @Override
    public RecommendUser findPersonalInfo(Long toUserId, Long jiarenId) {
        // 1.构建条件
        Query query = new Query(
                Criteria.where("toUserId").is(toUserId)
                .and("userId").is(jiarenId)
        );
        // 3.查询并返回
        return mongoTemplate.findOne(query, RecommendUser.class);
    }
}
